package org.jyrcp.base;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart
{
	private ViewCreator pythonViewCreator = null;
	private Object viewer;

	public void init(IViewSite site) throws PartInitException
	{
		super.init(site);

		String packageName = TextUtils.shortenPackageName(site.getId());
		String className = TextUtils.packageNameToClassName(packageName);

		JyRCPApplication.interpreter.exec("from views." + packageName
				+ " import " + className);
		JyRCPApplication.interpreter.exec("view = " + className + "()");

		this.pythonViewCreator = (ViewCreator) JyRCPApplication.interpreter.get(
				"view", ViewCreator.class);
	}

	public void createPartControl(Composite parent)
	{
		this.viewer = this.pythonViewCreator.createPartControl(parent);
		if (this.viewer instanceof Viewer)
		{
			((Viewer) this.viewer).setInput(getViewSite());
			getSite().setSelectionProvider(((Viewer) this.viewer));
		}
	}

	public void setFocus()
	{
		if (this.viewer instanceof Composite)
		{
			((Composite) this.viewer).setFocus();
		}
	}
}